package pv.control;

import java.util.List;

import pv.dto.SubCategoryDTO;
import pv.finder.R;
import android.content.Context;
import android.graphics.Color;
import android.hardware.usb.UsbAccessory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SubCategoryAdapter implements View.OnClickListener {

	List<SubCategoryDTO> listData;
	Context context;
	protected int selectedItem;

	SubCategoryView layout;

	public SubCategoryView getLayout() {
		return layout;
	}

	public void setLayout(SubCategoryView layout) {
		this.layout = layout;
	}

	public SubCategoryAdapter(Context context, List<SubCategoryDTO> listData) {
		this.context = context;
		this.listData = listData;
		selectedItem = 0;
	}

	public SubCategoryAdapter(Context context) {
		this.context = context;
		selectedItem = 0;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return listData != null ? listData.size() : 0;
	}

	public SubCategoryDTO getItem(int position) {
		// TODO Auto-generated method stub
		return listData != null ? listData.get(position) : null;
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public void setListData(List<SubCategoryDTO> listData) {
		this.listData = listData;
	}

	public View getView(LayoutInflater inflater, int position,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view;
		if (convertView != null) {
			view = convertView;

		} else {
			// LayoutParams params = new LayoutParams(
			// FrameLayout.LayoutParams.WRAP_CONTENT,
			// FrameLayout.LayoutParams.WRAP_CONTENT);
			// TextView tv = new TextView(context);
			// tv.setLayoutParams(params);
			// tv.setBackgroundResource(R.drawable.custon_button);
			// tv.setText(listData.get(position).getName());
			// view = tv;
			view = getView(inflater, position);

		}
		// TODO do what next if pass parent and converView

		return view;
	}

	public View getView(LayoutInflater inflater, int position) {
		// TODO Auto-generated method stub

		// LinearLayout viewGroup = new LinearLayout(context);
		//
		// LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
		// FrameLayout.LayoutParams.WRAP_CONTENT,
		// FrameLayout.LayoutParams.WRAP_CONTENT);
		// viewGroup.setLayoutParams(params);
		//
		// TextView tv = new TextView(context);
		// tv.setLayoutParams(new
		// LayoutParams(TextView.LayoutParams.WRAP_CONTENT,
		// TextView.LayoutParams.WRAP_CONTENT));
		// tv.setText(listData.get(position).getName());
		// tv.setTextAppearance(context, R.style.text_normal);
		//
		// viewGroup.addView(tv);
		// viewGroup.setPadding(100, 100, 100, 100);
		//
		// viewGroup.setBackgroundResource(R.drawable.sub_category);
		View view = inflater.inflate(R.layout.sucategory, null);
		((TextView) view.findViewById(R.id.txtSubName)).setText(listData.get(
				position).getName());
		view.setSelected(selectedItem == position);
		view.setTag(position);
		view.setOnClickListener(this);

		return view;
	}

	public SubCategoryDTO getSelectedItem() {
		return listData.get(selectedItem);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		View unselectedView = layout.getChildAt(selectedItem);
		unselectedView.setSelected(false);
		selectedItem = (Integer) v.getTag();
		View selectedView = layout.getChildAt(selectedItem);
		selectedView.setSelected(true);
		layout.onSubCategorySelected(selectedItem);

	}

}
